/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.example.quickstart;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageClientIDSetter;
import org.apache.rocketmq.example.util.RocketMQPrinter;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

/**
 * This class demonstrates how to send messages to brokers using provided {@link DefaultMQProducer}.
 */
public class Producer {

    /**
     * The number of produced messages.
     */
    public static final int MESSAGE_COUNT = 10;
    public static final String PRODUCER_GROUP = "please_rename_unique_group_name";
    public static final String DEFAULT_NAMESRVADDR = "127.0.0.1:9876";
    public static final String TOPIC = "DelayTopic" + LocalDateTime.now().getDayOfYear() + "_" + LocalDateTime.now().getHour();
    public static final String TAG = "NormalTag";

    public static void main(String[] args) throws MQClientException, InterruptedException {

        /*
         * Instantiate with a producer group name.
         */
        DefaultMQProducer producer = new DefaultMQProducer(PRODUCER_GROUP);
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.setDefaultTopicQueueNums(10);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            producer.shutdown();
        }));
        /*
         * Specify name server addresses.
         *
         * Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR
         * <pre>
         * {@code
         *  producer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876");
         * }
         * </pre>
         */
        // Uncomment the following line while debugging, namesrvAddr should be set to your local address
        // producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);

        /*
         * Launch the instance.
         */
        producer.setAutoBatch(true);
        producer.start();
        Scanner scanner = new Scanner(System.in);
        int age  = 0;
        age= scanner.nextInt();
           while(age!=10){

               if(age == 0){
                   List<Message> msgs = new java.util.ArrayList<Message>();
                   for (int i = 0; i < MESSAGE_COUNT; i++) {

                       MessageClientIDSetter.createUniqID();
                       /*
                        * Create a message instance, specifying topic, tag and message body.
                        */
                       Message msg = null;
                       try {
                           msg = new Message(
                                   TOPIC /* Topic */,
                                   TAG /* Tag */,
                                   ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
                           );
                       } catch (UnsupportedEncodingException e) {
                           throw new RuntimeException(e);
                       }
                       msgs.add(msg);
                   }

                   SendOrNot(producer, msgs);


               }else {
                   for (int i = 0; i < MESSAGE_COUNT; i++) {

                       MessageClientIDSetter.createUniqID();
                       /*
                        * Create a message instance, specifying topic, tag and message body.
                        */
                       Message msg = null;
                       try {
                           msg = new Message(
                                   TOPIC /* Topic */,
                                   TAG /* Tag */,
                                   ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
                           );
                       } catch (UnsupportedEncodingException e) {
                           e.printStackTrace();
                       }
                       msg.setDelayTimeSec(60 * 10);
                       SendOrNot(producer, msg);

                   }
               }


               age = scanner.nextInt();
           }


           producer.shutdown();

        /*
         * Shut down once the producer instance is no longer in use.
         */

    }

    private static void SendOrNot(DefaultMQProducer producer, List<Message> msgs) throws MQClientException, InterruptedException {
        SendResult sendResult = null;
        try {
            sendResult = producer.send(msgs, 20 * 1000);
        } catch (RemotingException e) {
            throw new RuntimeException(e);
        } catch (MQBrokerException e) {
            throw new RuntimeException(e);
        }
        System.out.println("");
        System.out.printf("%s%n\n", sendResult);
    }
    private static void SendOrNot(DefaultMQProducer producer, Message msg) throws MQClientException, InterruptedException {
        SendResult sendResult = null;
        try {
            sendResult = producer.send(msg, 20 * 1000);
        } catch (RemotingException e) {
            throw new RuntimeException(e);
        } catch (MQBrokerException e) {
            throw new RuntimeException(e);
        }
        RocketMQPrinter.printSendResult(sendResult);

    }
}
